#include <stdio.h>
#include <stdlib.h>
#include "coro.h"
#include "fs_walk.h"
#include <string.h>

void usage()
{
    puts("find -name file-name dir");
}

char *aim;
char *dir_path;
//extern void fs_walk();

coro_t *coro_d;
coro_t *coro_f;

void find_file()
{
	while(1) {
		char *path = (char *)coro_resume(coro_d);
		if(path == NULL) break;
		
		int length = strlen(aim);
        	int j = strlen(path);
        	int i;
        	int temp = 0;
        	for (i = 0; i < length; i++) {
            	if(path[j - i] == aim[length - i])
                	temp += 1;
            	else break;
        	}
		if(temp == length)
			printf("%s\n", path);
	}
	exit(0);
}

int main(int argc, char *argv[])
{
    /*aim = malloc(strlen(argv[2]));
    strcpy(aim, argv[2]);
    path = malloc(strlen(argv[1]));
    strcpy(path, argv[1]);*/
    
    aim = argv[2];
    dir_path = argv[1];
    
    coro_d = coro_new(fs_walk);
    coro_f = coro_new(find_file);

    printf("BEGIN\n");
    coro_boot(coro_f);

    return 0;
}
